﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace GIMS.Exec.Test
{
    /// <summary>
    ///This is a test class for ExecutorTest and is intended
    ///to contain all ExecutorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ExecutorTest
    {
        private static string tempImagePath;
        private static string tempPath;

        [ClassInitialize()]
        public static void Initialize(TestContext testContext)
        {
            tempPath = Path.Combine(Path.GetTempPath(), "GIMSTest");
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }

            tempImagePath = Path.Combine(tempPath, "amplifier.jpg");
            if (!File.Exists(tempImagePath))
            {
                Resource.amplifier.Save(tempImagePath);
            }
        }

        [ClassCleanup]
        public static void CleanUp()
        {
            if (Directory.Exists(tempPath))
            {
                Directory.Delete(tempPath, true);
            }
        }

        /// <summary>
        ///A test for CreatePreview
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.Executor.dll")]
        public void CreatePreviewTest()
        {
            Executor executor = new Executor();

            string command = tempImagePath + " -resize 20%";
            string outputPath = executor.CreatePreview("convert", command);
            Assert.IsTrue(File.Exists(outputPath)); //Has the file been created?
        }

        /// <summary>
        ///A test for ExecuteCommand
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.Executor.dll")]
        public void ExecuteCommandTest()
        {
            Executor executor = new Executor();

            string program = "convert";
            string outputPath = Path.Combine(tempPath, Guid.NewGuid() + ".jpg");
            string command = tempImagePath + " -resize 20%";
            string completeCommand = string.Concat(command, " ", outputPath);
            executor.ExecuteCommand(program, completeCommand);
            Assert.IsTrue(File.Exists(outputPath)); //Has the file been created?
        }
        
        /// <summary>
        ///A test for ExecuteCommand with exception
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(IMFileNotFoundException))]
        [DeploymentItem("GIMS.Executor.dll")]
        public void ExecuteCommandWithExceptionTest()
        {
            Executor executor = new Executor();

            string program = "convert";
            string outputPath = Path.Combine(tempPath, Guid.NewGuid() + ".jpg");
            string command = "asdf.bmp" + " -resize 20%";
            string completeCommand = string.Concat(command, " ", outputPath);
            try
            {
                executor.ExecuteCommand(program, completeCommand);
            }
            catch(IMFileNotFoundException ex)
            {
                Assert.IsTrue(ex.Message.StartsWith("ImageMagick message: convert.exe: unable to open image"));
                throw;
            }     
        }

        /// <summary>
        ///A test for ExecuteCommand with override
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.Executor.dll")]
        public void ExecuteCommandWithOverrideTest()
        {
            Executor executor = new Executor();

            string program = "convert";
            string outputPath = Path.Combine(tempPath, Guid.NewGuid() + ".jpg");
            string command = tempImagePath + " -resize 20%";
            string completeCommand = string.Concat(command, " ", outputPath);

            Resource.amplifier.Save(outputPath);
            var fi = new FileInfo(outputPath);
            var oriSize = fi.Length;

            //override
            executor.ExecuteCommand(program, completeCommand);
            fi = new FileInfo(outputPath);
            var newSize = fi.Length;
            Assert.IsTrue(newSize < oriSize);
        }

        /// <summary>
        ///A test for the Property TempPath
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.Executor.dll")]
        public void TempPathCreateDirectoryTest()
        {
            Executor_Accessor executor = new Executor_Accessor();
            string tempPath = executor.TempPath;
            //first delete if necessary
            if (Directory.Exists(tempPath))
            {
                Directory.Delete(tempPath, true);
            }
            Assert.IsFalse(Directory.Exists(tempPath));
            //then recreate by using the property
            executor.tempPath = null;
            tempPath = executor.TempPath;
            Assert.IsTrue(Directory.Exists(tempPath));
        }
    }
}
